<!--
 * @Descripttion: 
 * @version: @ 1.0.0
 * @Author: ZJZ
 * @Date: 2021-05-19 08:42:14
 * @LastEditors: 朱江洲
 * @LastEditTime: 2021-12-01 15:54:55
-->
<template>
  <div>
    <el-form
      :model="setting"
      ref="setting"
      :rules="setingRules"
      :label-position="labelPosition"
      label-width="80px"
      :inline="false"
      size="normal"
    >
      <el-form-item label="标识名" prop="name">
        <el-input v-model="seting.name" style="width: 330px"></el-input>
      </el-form-item>
      <el-form-item label="提示文字">
        <el-input v-model="seting.inputTips"></el-input>
      </el-form-item>
      <el-form-item label="字段来源">
        <el-select
          v-model="seting.fieldType"
          placeholder="请选择字段来源"
          style="width: 330px"
        >
          <el-option
            v-for="(item, index) in option"
            :key="index"
            :label="item.label"
            :value="item.fieldType"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选项设置" v-if="seting.type == 3">
        <div
          class="sel"
          v-for="(item, index) in seting.labelOptions"
          :key="index"
        >
          <el-input v-model="item.label" style="width: 120px"></el-input>
          <i @click="addChe" class="el-icon-circle-plus-outline"></i>
          <i @click="reduceChe(index)" class="el-icon-remove-outline"></i>
        </div>
      </el-form-item>
      <el-form-item label="选项设置" v-if="seting.type == 4">
        <el-input v-model="seting.value" style="width: 120px"></el-input>
        <el-input
          v-model="seting.value1"
          style="width: 120px; margin-left: 10px"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="转换学员信息字段"
        v-if="seting.fieldType == '' && seting.type == 5"
      >
        <div
          class="sel"
          v-for="(item, index) in seting.labelOptions"
          :key="index"
        >
          <el-input v-model="item.label" style="width: 120px"></el-input>
          <i @click="Add" class="el-icon-circle-plus-outline"></i>
          <i @click="reduce(index)" class="el-icon-remove-outline"></i>
        </div>
      </el-form-item>
      <el-form-item label="是否必填">
        <el-checkbox v-model="seting.isNull"> 必填 </el-checkbox>
      </el-form-item>
      <el-form-item label="是否启用">
        <el-checkbox v-model="seting.isHidden"> 启用 </el-checkbox>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getFieldSource } from "@/api/signUp";
export default {
  props: {
    seting: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      setting: {},
      option: [],
      labelPosition: "top",
      setingRules: {
        name: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
      },
    };
  },
  watch: {
    seting: {
      deep: true,
      handler(val) {
        if (val) {
          if (val.type == 1) {
            if (val.isNull == true) {
              val.rules.text.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.text.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 2) {
            if (val.isNull == true) {
              val.rules.rowText.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.rowText.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 3) {
            if (val.isNull == true) {
              val.rules.choiceList.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.choiceList.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 4) {
            if (val.isNull == true) {
              val.rules.radio.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.radio.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 5) {
            if (val.isNull == true) {
              val.rules.dropDown.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.dropDown.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 6) {
            if (val.isNull == true) {
              val.rules.num.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.num.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 7) {
            if (val.isNull == true) {
              val.rules.phone.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.phone.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 8) {
            if (val.isNull == true) {
              val.rules.mailbox.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.mailbox.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 9) {
            if (val.isNull == true) {
              val.rules.fileImg.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.fileImg.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 10) {
            if (val.isNull == true) {
              val.rules.fileList1.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.fileList1.map((item) => {
                item.required = false;
              });
            }
          } else if (val.type == 11) {
            if (val.isNull == true) {
              val.rules.dates.map((item) => {
                item.required = true;
              });
            } else {
              val.rules.dates.map((item) => {
                item.required = false;
              });
            }
          }
          this.setting = val;
          getFieldSource({
            examType: val.type,
          }).then((res) => {
            let arr = res.rows;
            this.option = arr;
          });
        }
      },
    },
  },
  methods: {
    Add() {
      if (this.seting.disab == 0) return;
      this.seting.labelOptions.push({
        value: this.seting.labelOptions.length - 0 + 1,
        label: "选项" + (this.seting.labelOptions.length - 0 + 1),
      });
    },
    reduce(i) {
      if (this.seting.disab == 0) return;
      if (this.seting.labelOptions.length > 1) {
        this.seting.labelOptions.splice(i, 1);
      }
    },
    addChe() {
      this.seting.labelOptions.push({
        value: this.seting.labelOptions.length - 0 + 1,
        label: "选项" + (this.seting.labelOptions.length - 0 + 1),
      });
    },
    reduceChe(i) {
      if (this.seting.labelOptions.length > 1) {
        this.seting.labelOptions.splice(i, 1);
      }
    },
  },
};
</script>

<style lang="less" scoped>
/deep/ .el-form-item__label {
  line-height: 20px;
}
.sel {
  display: flex;
  margin-top: 10px;
  i {
    font-size: 25px;
    margin-top: 8px;
    margin-left: 10px;
  }
}
</style>